<?php

class CommunityController extends Controller
{
	public $layout='//layouts/community';
	
	/**
	 * 社区首页
	 */
	public function actionIndex()
	{
		// 新内容
		$nodeNew = Node::model()->findAll('status = '.DefineBox::NODE_STATUS_NORMAL.' order by id DESC limit 6');
		// 待撰内容
		$nodeEmpty = Node::model()->findAll('status = '.DefineBox::NODE_STATUS_EMPTY.' order by id DESC limit 6');
		//投票区
		$pollNew = Poll::model()->findAll('status = 1 order by id DESC limit 6');
		//最新内容讨论
		$nodeTalkNew = NodeTalk::model()->with('node')->findAll(array('order'=>'post_time DESC', 'limit'=>6));
		// 社区公告
		$topicNotice = Topic::model()->findAll('tid = '.DefineBox::TOPIC_TYPE_NOTICE.' order by create_time DESC limit 6');
		// 社区事务
		$topicWork = Topic::model()->findAll('tid = '.DefineBox::TOPIC_TYPE_WORK.' order by create_time DESC limit 6');
		// 创意风暴
		$topicIdea = Topic::model()->findAll('tid = '.DefineBox::TOPIC_TYPE_IDEA.' order by create_time DESC limit 6');
		
		//让index视图进行渲染
		$this->render('index',array(
				'nodeNew' => $nodeNew,
				'nodeEmpty' => $nodeEmpty,
				'pollNew' => $pollNew,
				'nodeTalkNew' => $nodeTalkNew,
				'topicNotice' => $topicNotice,
				'topicWork' => $topicWork,
				'topicIdea' => $topicIdea,
		));
	}
	
	/**
	 * 成熟度投票
	 * @param unknown_type $nid
	 * @param unknown_type $tid
	 */
	public function actionPollLevel($nid, $tid)
	{
		$row = Node::model()->findByPk($nid);
		$model = new Poll;
		$model->value = $row->level;
		if(isset($_POST['Poll'])){
			$model->value = $_POST['Poll']['value'];
			$model->description = $_POST['Poll']['description'];
			$sm = substr($_POST['Poll']['start_time'],5,2);
			$sd = substr($_POST['Poll']['start_time'],8,2);
			$sy = substr($_POST['Poll']['start_time'],0,4);
			$fm = substr($_POST['Poll']['finish_time'],5,2);
			$fd = substr($_POST['Poll']['finish_time'],8,2);
			$fy = substr($_POST['Poll']['finish_time'],0,4);
			$model->start_time = mktime(0,0,0,$sm, $sd, $sy);
			$model->finish_time = mktime(0,0,0,$fm, $fd, $fy);
			$model->uid = Yii::app()->user->id;
			$model->nid = $nid;
			$model->tid = $tid;
			if (time() <= $model->finish_time){
				$model->status = 1;
			}
			else{
				$model->status = 0;
				$row->status=DefineBox::NODE_STATUS_NORMAL;
				$row->save();
			}
			if($model->save()){
				$pollVote=new PollVote;
				$pollVote->pid=$model->id;
				$pollVote->uid = Yii::app()->user->id;
				$pollVote->value=1;
				$pollVote->reason=$model->description;
				$pollVote->save();
				$row->status=DefineBox::NODE_STATUS_LEVELPOLL;
				$row->save();
				$this->redirect(array('/node/view', 'id'=>$row->id));
			}
	
		}
		$this->render('/community/polllevel',array(
				'model'=>$model,
				'row'=>$row
		));
	}
	
	/**
	 * 显示投票情况
	 */
	public function actionViewPoll($id)
	{
		$poll = Poll::model()->findByAttributes(array('id'=>$id, 'status'=>1));
		$pollVote = PollVote::model()->with('user')->findAllByAttributes(array('pid'=>$id));
		if ($poll->status == 0){
	
		}
		$this->render('/community/viewpoll', array(
				'poll'=>$poll,
				'pollVote'=>$pollVote,
		));
	}
}